<?php
/**
 * Created by PhpStorm.
 * User: LiuTao
 * Date: 2018/11/12
 * Time: 17:05
 */

require_once("Thread.php");

abstract class Demo
{
    static function test($arg)
    {
        $count = 0;
        echo "Thread4 start :".PHP_EOL;
        do {
            echo "task with parameter '$arg' starts\n";
            sleep(rand(2, 5));    // wait for random seconds
            echo "task with parameter '$arg' ENDS\n";
            if ($count > 3) {
                exit();
            }
            $count++;
        } while (true);
        echo "Thread4 stop".PHP_EOL;

    }

    static function demo1($arg)
    {
        echo "Thread1 start :".PHP_EOL;
        echo $arg . PHP_EOL;
        echo "Thread1 stop".PHP_EOL;
    }

    static function demo2($arg, $arg2)
    {
        echo "Thread2 start :".PHP_EOL;
        echo $arg . PHP_EOL;
        sleep(rand(2, 5));
        echo "Thread2 start with sleep:".PHP_EOL;
        echo $arg2 . PHP_EOL;
        echo "Thread2 stop".PHP_EOL;

    }

    static function demo3(array $arr)
    {
        echo "Thread3 start :".PHP_EOL;
        var_dump($arr);
        echo "Thread3 stop".PHP_EOL;
    }

}

$thread1 = new Thread('Demo::demo1');
$thread1->start('demo1');

$thread1 = new Thread('Demo::demo2');
$thread1->start('demo2', 'demo22');

$thread1 = new Thread('Demo::demo3');
$thread1->start(['data' => 'demo3']);

$thread = new Thread('Demo::test');
$thread->start('one');
$thread->start('two');